home *** CD-ROM | disk | FTP | other *** search
/ Commodore Free 7 / Commodore_Free_Issue_07_2007_Commodore_Computer_Club.d64 / t.loadstar prg 1 < prev    next >
Text File  |  2023-02-26  |  15KB  |  609 lines

  1. uMr LOADSTAR's Intro to Programming
  2. the C64 Part 1
  3. By Dave Moorman
  4.  
  5. Introduction
  6.  
  7. The best place in the world to learn
  8. enjoy the art of computer programming
  9. is on the C-64. It is an old machine, &
  10. therefore a lot less complex than new
  11. computers. It has BASIC 2.0 built-in,
  12. plus many Machine Language (ML)
  13. routines that can be used from BASIC.
  14.  
  15. The processor is the MOS Technology
  16. 6510, a member of the 6502 family,
  17. which is very efficient. And the C-64
  18. was designed to be adaptable on many
  19. levels.
  20.  
  21. But mostly, it is inexpensive. A real
  22. C64 is always available on Ebay. Or,
  23. get the C64DTV & do some hardware
  24. hacking to add a disk drive & key-
  25. board. Or download the Versatile
  26. Commodore Emulator (VICE) from the web
  27. for free. Whichever way you go, you
  28. have a great platform for the kind of
  29. programs one person can sit down &
  30. write. And you can write them just the
  31. way you want.
  32.  
  33. I am going to make some assumptions.
  34. First, I must assume you have a C-64
  35. and a disk drive or VICE. I assume you
  36. know how to format a disk (on a real
  37. machine) or create & attach a disk
  38. image (with VICE). I even must assume
  39. that if you are using VICE, you know
  40. the different keys you must press for
  41. certain characters.
  42.  
  43. I also assume that beyond knowing how
  44. to LOAD & RUN a program, you have no
  45. idea how to go about writing one.
  46.  
  47. That is what we will do in this series
  48. of articles. So, fire up your C64. Get
  49. a new disk ready. Because HERE WE GO!
  50.  
  51. I DREAM OF GENIE IN A LIGHT BROWN BOX
  52.  
  53. On the blue screen, you see, in Light
  54. Blue, the word READY. Under it is a
  55. flashing block. This is the cursor. You
  56. probably know what a cursor is, but
  57. just to make sure here is a definition.
  58. The cursor is the place where the next
  59. character you type will appear on the
  60. screen.
  61.  
  62. Press [Home] & the cursor goes to the
  63. upper left corner of the screen. You
  64. can use the cursor keys to move the
  65. cursor around the screen. On the real
  66. C64, you have two cursor keys -- [Down]
  67. & [Right]. Press [Shift-Down] to go Up,
  68. [Shift-Right] to go Left. (VICE has it
  69. much better -- 4 keys!) I will refer to
  70. Cursor Up, Cursor Down, etc. You will
  71. get used to using the Shift!
  72.  
  73. Now press [Shift-Home]. This is [CLR],
  74. since it Clears the screen. Cursor
  75. around a bit to get used to it. Now,
  76. crack your knuckles, stretch, & prepare
  77. to meet the Genie!
  78.  
  79. Inside your computer is a magic genie
  80. who will perform any command you give
  81. it. Try it. Type:
  82.  
  83. DO MY TAXES
  84.  
  85. Nothing happened? That is because the
  86. genie doesn't know you are finished
  87. typing. With the cursor flashing on the
  88. same line, press [RETURN].
  89.  
  90. ?SYNTAX  ERROR
  91. READY
  92. []
  93.  
  94. Syntax Error" means "Huh? I don't
  95. understand." Fortunately, the genie is
  96. always forgiving. See, it is ready
  97. already.
  98.  
  99. The problem is that the genie under-
  100. stands only a few commands. You must
  101. spell the commands correctly -- because
  102. this is a stupid genie. So try this:
  103.  
  104. ?5+9
  105.  
  106. And press [RETURN]. (I won't mention it
  107. again. Whenever you are done typing,
  108. press [RETURN].)
  109.  
  110.  14
  111.  
  112. Pretty clever. What should we call a
  113. magic genie that is really good at
  114. computing math problems? Anyway, the
  115. Question Mark means Print. You could
  116. type out PRINT instead, but why not
  117. just use the single character?
  118.  
  119. You might have noticed that there is a
  120. space in front of the 1. That is to
  121. leave room for a minus sign, should one
  122. be necessary.
  123.  
  124. ?5-9
  125. -4
  126.  
  127. We have multiply & divide as well:
  128.  
  129. ?16/2*3
  130.  24
  131.  
  132. The [/] is Divide, [*] is Multiply.
  133. When the genie does math, it always
  134. multiplies & divides first, then adds
  135. and subtracts. For example:
  136.  
  137. ?3+2*5-1
  138.  
  139. does not equal 20.
  140.  
  141.  12
  142.  
  143. That is because 2*5 is calculated 1st,
  144. then 3+10-1, which results in 12.
  145. However, you can force one calculation
  146. before another.
  147.  
  148. ?(3+2)*(5-1)
  149.  20
  150.  
  151. There. Calculations in parentheses are
  152. always performed first. So, 3+2=5,
  153. 5-1=4, 5*4=20.
  154.  
  155. So, you have a calculator. Big deal!
  156. But wait. [?] means PRINT. What else
  157. can we print?
  158.  
  159. ?YOUR NAME
  160. 0
  161.  
  162. Ok, I fooled you again. Letters are
  163. used as VARIABLEs -- little boxes
  164. that contain values. The genie thinks
  165. you want to print the contents of a
  166. variable. But you want to print,
  167. literally, "YOUR NAME".
  168.  
  169. ?"YOUR NAME"
  170. YOUR NAME
  171.  
  172. I hope you used your own name! This
  173. time the genie printed out exactly what
  174. was between the double-quotes. If you
  175. did not use double-quotes [Shift-2] you
  176. probably got a SYNTAX ERROR. But if you
  177. were wrong, don't fret. The genie is
  178. always READY for you to do it right.
  179.  
  180. The characters between the double-
  181. quotes are said to be in a STRING,
  182. because they are strung together. In
  183. this case, it is a Literal String. What
  184. other kind of string is there?
  185.  
  186. N$="YOUR NAME"
  187.  
  188. When you pressed [RETURN] nothing
  189. happened? Do this:
  190.  
  191. ?N$
  192. YOUR NAME
  193.  
  194. N$ (pronounced "N string") is a String
  195. Variable. It is a box that contains a
  196. string. We also have Numeric
  197. Variables
  198.  
  199. N=1234
  200.  
  201. READY.
  202. ?N
  203.  1234
  204.  
  205. N & N$ are two different variables.
  206.  
  207. ?N$,N
  208. YOUR NAME  1234
  209.  
  210. Variables always begin with an alphabet
  211. character (A-Z) & can be 1 or 2 char-
  212. acters long. The 2nd character can be
  213. alpha or a number. A, AB, P0, S5, & RX
  214. are all numeric variables. String
  215. variables have the dollar sign after
  216. the characters. A$, AB$, P0$, S5$, &
  217. RX$ are all string variables. Remember
  218. to pronounce the dollar sign as
  219. "string."
  220.  
  221. IMMEDIATE vs PROGRAM: The Battle of the
  222. Modes
  223.  
  224. All this is fine & dandy, but so far we
  225. have nothing much more than a fancy
  226. calculator. That is because we have
  227. been working in IMMEDIATE Mode. That
  228. is, when you press [RETURN], the genie
  229. responds immediately. But we have
  230. another mode. Try this:
  231.  
  232. 10 N$="AMOUNT TENDERED"
  233.  
  234. Nothing happened, not even the READY.
  235. Actually, a lot happened inside the
  236. machine.
  237.  
  238. Back in 1976, Bill Gates & Paul Allen
  239. wrote the first BASIC operating system
  240. for the Altair 8800 microcomputer.
  241. Memory was expensive & at a premium. So
  242. a clever idea was developed to indicate
  243. whether what was typed was to be
  244. computed immediately, or put in program
  245. memory.
  246.  
  247. If the first character(s) of a line are
  248. numeric, the line is considered Program
  249. Mode. The text is placed into Program
  250. Memory, organized by "line numbers." To
  251. look at what is in your program, type
  252.  
  253. LIST
  254.  
  255. You will see...
  256.  
  257. 10 N$="AMOUNT TENDERED"
  258.  
  259. Add two more lines (press [RETURN] for
  260. each):
  261.  
  262. 5 N=35.75
  263. 20 ?N$;N
  264.  
  265. Now list the program again.
  266.  
  267. 5 N=35.75
  268. 10 N$="AMOUNT TENDERED"
  269. 20 PRINTN$;N
  270.  
  271. You have just written a program! To
  272. watch it run, type
  273.  
  274. RUN
  275.  
  276. You should see:
  277.  
  278. AMOUNT TENDERED 35.75
  279.  
  280. This is just a beginning!
  281.  
  282. SAVING & LOADING YOUR PROGRAMS
  283.  
  284. Now that you are an honest to goodness
  285. programmer, you will need to save your
  286. program to your disk (presumably in
  287. drive 8 & formatted). The quick way is
  288. to type:
  289.  
  290. SAVE"MYPROG",8
  291.  
  292. The filename is "MYPROG" & you are
  293. saving it on drive #8. File names can
  294. be up to 16 characters in length. You
  295. can verify that the program has been
  296. correctly saved with:
  297.  
  298. VERIFY"MYPROG",8
  299.  
  300. But this is usually not necessary, if
  301. your drive & disk are in good condition
  302. Now you can turn your computer off,
  303. back on, & load your program:
  304.  
  305. LOAD"MYPROG",8
  306.  
  307. then
  308.  
  309. LIST & RUN
  310.  
  311. Now for some tricky stuff. If you
  312. change your program, you cannot
  313. simply save it to the same file name.
  314. The file is already on the disk & must
  315. be scratched first. BASIC 2.0 does
  316. have a SAVE@ command, but this has
  317. proven to be buggy, so don't use it!
  318. You can save your changes to a
  319. different filename:
  320.  
  321. SAVE"MYPROG1",8
  322.  
  323. But we at LOADSTAR have a better way.
  324. The following code will not make a lot
  325. of sense to you, but that isn't a
  326. problem. Once you type it in exactly as
  327. shown, you will have a SHELL program
  328. you can use for all programs. Type:
  329.  
  330. NEW
  331.  
  332. To clear the memory, then type this:
  333.  
  334. 60000 N$="SHELL"
  335. 60001 OPEN1,8,15,"S0:"+N$:CLOSE1
  336. 60002 SAVEN$,8
  337.  
  338. Once you have entered the lines
  339. (Pressing [RETURN] after each), type:
  340.  
  341. GOTO60000
  342.  
  343. The program, named "SHELL" will be
  344. saved to your disk. Whenever you start
  345. a program, first
  346.  
  347. LOAD"SHELL",8
  348.  
  349. LIST it, & change the string in line
  350. 60000. One of the neat things about the
  351. C64 is its screen editor. To edit a
  352. line, all you have to do is list it,
  353.  
  354. LIST60000
  355.  
  356. Move your cursor up to the place you
  357. want to edit, & type over the text.
  358. Pressing [RETURN] (regardless where it
  359. is on the line) will put the edited
  360. line in memory. So when you start a new
  361. program, make line 60000 read:
  362.  
  363. 60000 N$="NEW NAME"
  364.  
  365. whatever the new name might be. Then
  366. do the GOTO60000.
  367.  
  368. A bit of history here. Long ago, I was
  369. working on three interrelated programs
  370. (I will call them PROG1, PROG2, &
  371. PROG3). I had just made some correct-
  372. ions in PROG3, but accidentally saved
  373. it as PROG1. Suddenly, PROG1 was gone.
  374. Hours of programming went where all bad
  375. little files go. I realized I needed a
  376. better way!
  377.  
  378. My answer was to create a "scratch &
  379. save" routine, with the program name
  380. embedded in the program itself. I chose
  381. line 60000 since BASIC only handles
  382. line number between 0 & 63999. Being
  383. line 60000, the routine is always at
  384. the bottom of the program.
  385.  
  386. If some mistake or glitch messes up the
  387. code, my line 60000 will be garbled & I
  388. won't be able to save the gobblety-
  389. gook. (There is nothing worse than
  390. accidentally saving corrupted code. You
  391. are very unlikely to revive it. It is
  392. an occasion for a grown man to cry!)
  393.  
  394. With this routine, every time I save
  395. every program, I use exactly the same
  396. "command:" GOTO60000. My fingers know
  397. this command by heart. Now as I write,
  398. I do a save after entering every few
  399. lines, & especially before I run my
  400. program. I later learned that the gurus
  401. at LOADSTAR had come up with the same
  402. trick, except they used line 10000. The
  403. particular number doesn't matter much
  404. -- just use the same line number all
  405. the time.
  406.  
  407. Here is what the routine does. First,
  408. you put the program filename in N$.
  409. Then, you use a disk command to scratch
  410. the filename. Lastly, you save the
  411. filename. Again, we will get into the
  412. specifics later in this series. For
  413. now, just use "SHELL" (& change line
  414. 60000) to begin all new programs.
  415.  
  416. READING THE DISK DIRECTORY
  417.  
  418. Corners had to be cut to put all the
  419. power of BASIC 2.0 in a minimum of
  420. memory. Once such corner is that there
  421. is no Directory command. To see what
  422. is on the disk, you must
  423.  
  424. 1.Save what you are doing (if anything)
  425. 2.LOAD"$",8
  426. 3.LIST
  427.  
  428. You will see something like this:
  429.  
  430. 0 ["DISK NAME             " 98 2A]
  431. 1    "MYPROG"                 PRG
  432. 1    "SHELL"                  PRG
  433.  
  434. The top line (in reverse) is the disk
  435. header -- the name you gave the disk
  436. when you formatted it. The number to
  437. the left on the next two lines is the
  438. Block Size of the file. A disk block is
  439. 254 bytes in size -- & a 1541 disk has
  440. 664 blocks available. Following the
  441. block size is the filename. At the
  442. right is the type of the file. PRG
  443. means Program. The last line tells how
  444. many blocks are available on the disk.
  445.  
  446. Once you have looked at your directory,
  447. you can reload your program. Clumsy,
  448. yes -- but clever. The directory uses
  449. the same code as a program list, which
  450. saves memory. The only hassle is that
  451. looking at the directory destroys what-
  452. ever you have in program memory at the
  453. time. But of course, you DID save it.
  454. Right?
  455.  
  456. BACK TO BASIC
  457.  
  458. It is time to get back to the task at
  459. hand -- learning how to make the
  460. computer do your bidding. Load up
  461. SHELL, list line 60000, & change the
  462. name to "HELLO". You must be as tired
  463. of the all-caps as I am of typing all-
  464. caps. Press [C=-Shift] (that's
  465. [Commodore Logo-Shift]) to switch to
  466. upper/lower case characters. Let's get
  467. to work!
  468.  
  469. 10 ?"[clr]"
  470. 20 ?"[down]"
  471. 30 ?"Hello, World!"
  472.  
  473. RUN
  474.  
  475. There you go! If you list your program,
  476. you will notice that the ?'s have
  477. become PRINT. And PRINT is a very
  478. powerful command in BASIC! When you
  479. press the double-quote, the computer
  480. enters Quote Mode. Anything you type
  481. (other than [RETURN] or ["]) will be
  482. embedded in the string. So, [clr] means
  483. [Shift-Home], & it clears the screen.
  484. [down] is the Cursor Down key.
  485.  
  486. You can also insert text color changes
  487. right in the string.
  488.  
  489. 30 ?"[ctrl-1]H[ctrl-2]e[ctrl-3]l[ctrl-
  490. 4]l[ctrl-5]o, World!"
  491.  
  492. With this, each character in "Hello"
  493. will have a different color. Here are
  494. the various color controls (which may
  495. be printed on your number keys:
  496.  
  497. # CTRL      C=
  498. 1 Black   Orange
  499. 2 White   Brown
  500. 3 Red     Light Red
  501. 4 Cyan    Dark Gray
  502. 5 Purple  Med Gray
  503. 6 Green   Light Green
  504. 7 Blue    Light Blue
  505. 8 Yellow  Light Gray
  506.  
  507. While we are talking about colors, we
  508. must say we do not have any BASIC
  509. command to change the background or
  510. border color. However, we do have an
  511. all-purpose command that puts infor-
  512. mation right into memory -- POKE. And
  513. the color of the screen background &
  514. border are controlled by two locations
  515. in memory:
  516.  
  517. 53280 - Border Color
  518. 53281 - Background Color
  519.  
  520. So,
  521.  
  522. 25 poke 53281,14
  523. 26 poke 53280,0
  524.  
  525. will change the background to Light
  526. Blue & the border to Black. The numbers
  527. you poke for color are
  528.  
  529. 0 - Black   8 - Orange
  530. 1 - White   9 - Brown
  531. 2 - Red    10 - Light Red
  532. 3 - Cyan   11 - Dark Gray
  533. 4 - Purple 12 - Med. Gray
  534. 5 - Green  13 - Light Green
  535. 6 - Blue   14 - Light Blue
  536. 7 - Yellow 15 - Light Gray
  537.  
  538. As in Immediate Mode, you can print
  539. strings (literal or variables) or
  540. values (constants -- the actual numbers
  541. -- or variables). Numbers are printed
  542. with a preceding space & followed by a
  543. cursor right. Strings are printed
  544. exactly as they appear between the
  545. double-quotes. This is a good time for
  546. you to play around with the PRINT
  547. command. We can print several things
  548. on the same line by using the [;]
  549. (semi-colon) as a separator.
  550.  
  551. 29 n$="Dave"
  552. 30 ?"Hello, ";n$
  553. 31 age=57
  554. 32 ?"You are";age;"years old."
  555.  
  556. Normally, the PRINT command adds a
  557. "carriage return" at the end of each
  558. line. This means that the cursor moves
  559. down to the next line & to the left
  560. edge of the screen. A semi-colon
  561. "defeats" the carriage return.
  562.  
  563. So, if you want to print several things
  564. on one line but with different PRINT
  565. commands, put a semi-colon after the
  566. first printed line. Change line 30
  567. above to add a semi-colon after n$, &
  568. see what happens. You will have to
  569. insert a space on line 32 by placing
  570. the cursor over the Y & pressing
  571. [Shift-Ins/Del] -- to add space between
  572. the name & then next sentence. You
  573. can use a comma rather than the semi-
  574. colon, & the cursor will be moved to
  575. the next "tab" column. Try it out &
  576. see how it works. Another way to put
  577. the text where you want it on a line is
  578. with TAB.
  579.  
  580. 40 ?tab(15)"This is nearly centered"
  581.  
  582. Again, the only way to get acquainted
  583. with the commands & controls is to play
  584. with them. We have a whole slew of
  585. graphic characters available by
  586. pressing the C= key & a letter key. Try
  587. them out. See if you can draw a box.
  588. Another fun exercise is to develop
  589. large letters:
  590.  
  591. 50 ?"[c=-r][space][c=-r][space][c=-r]
  592. 51 ?"[c=-q][shift-*][c=-w][space]
  593.   [shift-minus]
  594. 52 ?"[c=-e][space][c=-e][space][c=-e]
  595.  
  596. You can create almost any letter using
  597. [C=-Q], [C=-W], [C=-E], [C=-R], [C=-
  598. A], [C=-S], [C=-Z], [C=-X], [Shift-*],
  599. & [Shift-minus] in three layers. The
  600. embedded graphic & control characters
  601. are incredibly difficult to write
  602. about, so I will leave such things up
  603. to you. I am here, after all, to show
  604. you how to program!
  605.  
  606. And program we will! So, play around.
  607. Have some fun. We will do some serious
  608. computing in the next episode!
  609.